package com.android.wordbook;
//参考代码地址：
// https://blog.csdn.net/qq_45901004/article/details/111242201?spm=1001.2014.3001.5501
// https://github.com/JinFengHua/WordTest
//悬浮按钮参考https://blog.csdn.net/gaolh89/article/details/79759404

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    DBHelper helper = null;
    private Activity activity;
    private EditText etQuery;//搜索内容
    private RecyclerView rvWordList;//列表
    private FloatingActionButton btnAdd;//添加按钮
    private WordAdapter wordAdapter;//列表适配器
    private List<WordItem> list;//全部数据
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity=this;
        setContentView(R.layout.activity_main);
        helper = new DBHelper(activity);
        etQuery=findViewById(R.id.et_query);
        rvWordList=findViewById(R.id.rv_word_list);
        btnAdd=findViewById(R.id.btn_add);

        LinearLayoutManager layoutManager=new LinearLayoutManager(activity);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvWordList.setLayoutManager(layoutManager);
        wordAdapter=new WordAdapter(activity);
        rvWordList.setAdapter(wordAdapter);
        wordAdapter.setItemListener(new WordAdapter.ItemListener() {
            @Override
            public void ItemClick(WordItem wordItem) {
                Intent intent = new Intent(activity, AddActivity.class);
                intent.putExtra("word", wordItem);
                startActivity(intent);
            }

            @Override
            public void ItemLongClick(WordItem wordItem) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
                dialog.setMessage("确认要删除该数据吗");
                dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SQLiteDatabase db = helper.getWritableDatabase();
                        if (db.isOpen()) {
                            db.execSQL("delete from word where id = "+ wordItem.getId());
                            db.close();
                        }
                        Toast.makeText(activity,"删除成功",Toast.LENGTH_LONG).show();
                        loadData("");
                    }
                });
                dialog.setNeutralButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                dialog.show();
            }
        });
        loadData("");
        setViewListener();
    }

    private void setViewListener() {
        //添加按钮
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到添加页面
                Intent intent=new Intent(activity,AddActivity.class);
                startActivity(intent);
            }
        });
        etQuery.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                loadData(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    //查询单词
    private void loadData(String content) {
        list = new ArrayList<>();
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor ;
        if ("".equals(content)) {
            String sql = "select * from word";
            cursor = db.rawQuery(sql, null);
        }else {
            String sql = "select * from word where word like ?";
            cursor = db.rawQuery(sql, new String[]{"%"+content+"%"});
        }

        if (cursor != null && cursor.getColumnCount() > 0) {
            while (cursor.moveToNext()) {
                Integer dbId = cursor.getInt(0);
                String word = cursor.getString(1);
                String note = cursor.getString(2);
                WordItem wordItem1 = new WordItem(dbId,word, note);
                list.add(wordItem1);
            }
        }
        db.close();
        wordAdapter.addItem(list);
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData("");
    }
}
